#!/usr/bin/env bash
##===----------------------------------------------------------------------===##
##
## This source file is part of the Swift.org open source project
##
## Copyright (c) 2025 Apple Inc. and the Swift project authors
## Licensed under Apache License v2.0 with Runtime Library Exception
##
## See https://swift.org/LICENSE.txt for license information
## See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
##
##===----------------------------------------------------------------------===##

# This is a shell script that generates a Swift source code file which contains
# the list of IANA "Character Sets".

set -eu

declare -r commandName="$(basename "$0")"
declare -r utilsDir="$(cd "$(dirname "$0")" && pwd)"
declare -r foundationRepoDir="$(cd "${utilsDir}/.." && pwd)"
declare -r targetSwiftFileRelativePath="Sources/FoundationEssentials/String/IANACharsetNames.swift"

declare -r copyrightYear=$(
  currentYear=$(date +%Y)
  if [[ $currentYear -eq 2025 ]]; then
    echo 2025
  else
    echo 2025-${currentYear}
  fi
)
declare -r swiftLicenseHeader="
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) ${copyrightYear} Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
"
declare -r warningComment="
// WARNING: DO NOT EDIT THIS FILE DIRECTLY.
//          This is auto-generated by \`${commandName}\`.

"

echo "Generating Swift source code..." 1>&2
declare generatedCode
generatedCode=$(
  echo "${swiftLicenseHeader##$'\n'}"
  echo "$warningComment"
  swift -D PRINT_CODE "${utilsDir}/${commandName}-impl.swift"
)

echo "Writing the code to '${targetSwiftFileRelativePath}'..." 1>&2
echo "$generatedCode" >"${foundationRepoDir}/${targetSwiftFileRelativePath}"

echo "Done." 1>&2
